Notebook Tour


In [1]:
from IPython.display import display, Image, HTML
from talktools import website, nbviewer

The Jupyter Notebook is a web-based application that enables users to create documents that combine live code wth narrative next, equations, images, visualizations and HTML/JavaScript widgets.

This notebook gives an overview of the Jupyter Notebook and the standard IPython Kernel for running Python code.

Interactive exploration

First and foremost, Jupyter is an interactive environment for writing and running code. We provide features to make this as pleasant as possible.

Tab completion:


In [2]:
import math

In [ ]:
math.

Interactive help:


In [3]:
math.cos?


Type:        builtin_function_or_method
String form: <built-in function cos>
Docstring:
cos(x)

Return the cosine of x (measured in radians).

Inline plotting:


In [4]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [5]:
plot(rand(50))


Out[5]:
[<matplotlib.lines.Line2D at 0x108753c50>]

Seamless access to the system shell:


In [6]:
ls -al


total 2856
drwxr-xr-x  37 bgranger  staff    1258 Oct 30 11:27 ./
drwxr-xr-x  41 bgranger  staff    1394 Oct 30 09:19 ../
-rw-r--r--@  1 bgranger  staff    8196 Oct 30 09:17 .DS_Store
drwxr-xr-x  15 bgranger  staff     510 Oct 30 08:17 .git/
-rw-r--r--   1 bgranger  staff     564 May 26 16:01 .gitignore
drwxr-xr-x  16 bgranger  staff     544 Oct 30 10:14 .ipynb_checkpoints/
-rw-------   1 bgranger  staff    1605 Oct 30 10:09 Bash Examples.ipynb
-rw-r--r--   1 bgranger  staff  378726 Aug 21 12:06 Components.ipynb
-rw-------   1 bgranger  staff    2822 Oct 30 10:29 Future Directions.ipynb
-rw-r--r--   1 bgranger  staff    2247 Oct 30 10:35 Index.ipynb
-rw-r--r--   1 bgranger  staff   13325 Oct 30 10:09 Interaction.ipynb
-rw-r--r--   1 bgranger  staff    6877 Oct 30 11:23 Jupyter and IPython.ipynb
-rw-r--r--   1 bgranger  staff    1082 May 26 16:01 LICENSE
-rw-r--r--   1 bgranger  staff    9173 Oct 30 10:10 Multilanguage.ipynb
-rw-r--r--   1 bgranger  staff   34350 Oct 30 11:27 Notebook Tour.ipynb
-rw-r--r--   1 bgranger  staff   15961 Oct 30 10:10 Notebook Usage.ipynb
-rw-------   1 bgranger  staff    2611 Oct 30 10:10 R Examples.ipynb
-rw-r--r--@  1 bgranger  staff     778 Aug 21 05:19 README.md
-rw-------   1 bgranger  staff   19732 Oct 30 11:20 Theme.ipynb
-rw-r--r--   1 bgranger  staff    5744 Aug 13 11:26 Tools for Open Science.ipynb
-rw-r--r--   1 bgranger  staff  401459 Oct 16 08:39 Visualization.ipynb
-rw-------   1 bgranger  staff  470277 Oct 30 11:18 What is Data Science.ipynb
drwxr-xr-x   3 bgranger  staff     102 Sep 18 14:51 __pycache__/
-rw-r--r--   1 bgranger  staff     886 Oct 30 09:28 courses.csv
drwxr-xr-x   4 bgranger  staff     136 Aug 21 05:28 data/
-rw-r--r--   1 bgranger  staff    1676 May 26 16:01 frontmatter.py
drwxr-xr-x  41 bgranger  staff    1394 Oct 30 09:13 images/
-rw-r--r--@  1 bgranger  staff    1152 Oct 16 08:56 ipythonproject.py
-rw-r--r--   1 bgranger  staff    1752 Oct 16 08:56 ipythonproject.pyc
drwxr-xr-x  17 bgranger  staff     578 May 26 16:01 ipythonteam/
-rw-r--r--   1 bgranger  staff     606 May 26 16:01 load_style.py
-rw-r--r--   1 bgranger  staff    1327 May 26 16:01 lorenz.py
-rw-r--r--   1 bgranger  staff    1850 May 28 11:59 lorenz.pyc
-rw-r--r--   1 bgranger  staff     972 May 26 16:01 talk.css
-rw-r--r--@  1 bgranger  staff    1090 May 26 16:01 talktools.py
-rw-r--r--   1 bgranger  staff    1554 May 27 13:34 talktools.pyc
-rw-r--r--   1 bgranger  staff      13 Aug 21 05:15 testing.md

Narrative text and equations

In addition to code cells, the Notebook offers Markdown cells, which enable the user to create narrative text with embedded LaTeX equations. Here is a cell that includes Maxwell's equations:

\begin{aligned} \nabla \times \vec{\mathbf{B}} -\, \frac1c\, \frac{\partial\vec{\mathbf{E}}}{\partial t} & = \frac{4\pi}{c}\vec{\mathbf{j}} \\ \nabla \cdot \vec{\mathbf{E}} & = 4 \pi \rho \\ \nabla \times \vec{\mathbf{E}}\, +\, \frac1c\, \frac{\partial\vec{\mathbf{B}}}{\partial t} & = \vec{\mathbf{0}} \\ \nabla \cdot \vec{\mathbf{B}} & = 0 \end{aligned}

Markdown cells enable users to create complex narratives that tell stories using code and data.

We are calling this literate computing as it is similar to Knuth's literate programming, but involves live code and data.

Rich output

Programming langauges, including Python, allow the writing of textual output to stdout and stderr. Python extends this idea slightly by allowing any Python object to declare its textual representation using special __str__ and __repr__ methods. When you print a Python object, these methods are automatically called:


In [7]:
import numpy as np

In [8]:
a = np.random.rand(10)
print a


[ 0.75442461  0.77398017  0.73133897  0.73842131  0.51821083  0.83111792
  0.88349024  0.24524433  0.29763714  0.74912639]

Jupyter and IPython extend this idea and allows objects to declare rich output representations:

  • JavaScript
  • HTML
  • LaTeX
  • PDF
  • PNG/JPEG
  • SVG

In IPython, the display function is like print for these rich representations:


In [9]:
from IPython.display import display

Images

The Image object has a JPEG/PNG representation that is rendered by the Notebook:


In [10]:
from IPython.display import Image

In [11]:
i = Image("images/jupyter_logo.png")

This representation is displayed if the object is returned from an expression:


In [12]:
i


Out[12]:

Or you can manually display the object using display:


In [13]:
display(i)


HTML

The HTML object has an HTML representation:


In [14]:
from IPython.display import HTML

In [15]:
s = """<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table>"""

In [16]:
h = HTML(s)

In [17]:
display(h)


Header 1 Header 2
row 1, cell 1 row 1, cell 2
row 2, cell 1 row 2, cell 2

JavaScript

The Javascript object has a "representation" that runs JavaScript code in the context of the Notebook.


In [18]:
from IPython.display import Javascript

In [19]:
display(Javascript("alert('hi');"))


LaTeX

This display architecture also understands objects that have a LaTeX representation. This is best illustrated by SymPy, which is a symbolic mathematics package for Python.


In [20]:
from __future__ import division
from sympy import *
x, y, z = symbols("x y z")
init_printing(use_latex='mathjax')

When a symbolic expression is passed to display or returned from an expression, the LaTeX representation is computed and displayed in the Notebook:


In [21]:
Rational(3,2)*pi + exp(I*x) / (x**2 + y)


Out[21]:
$$\frac{3 \pi}{2} + \frac{e^{i x}}{x^{2} + y}$$

In [22]:
(1/cos(x)).series(x, 0, 6)


Out[22]:
$$1 + \frac{x^{2}}{2} + \frac{5 x^{4}}{24} + \mathcal{O}\left(x^{6}\right)$$

nbconvert

nbconvert is a command line tool and Python package that can convert notebook documents to and from a wide range of formats:

  • PDF
  • Static HTML
  • Markdown
  • reveal.js slide shows

Convert this very notebook to static HTML:


In [ ]:
!ipython nbconvert --to html "Notebook Tour.ipynb"

nbviewer

nbviewer is a website for sharing Jupyter Notebooks on the web. It uses nbconvert to create a static HTML rendering of any notebook on the internet. This makes it easy to share notebooks with anyone in the world, without their having to install, or even know anything about, Jupyter or IPython.


In [ ]:
website('https://nbviewer.jupyter.org')

In [ ]: